package net.cyue.web.easyquery.provider.db;

import net.cyue.util.ResourceUtil;
import net.cyue.util.StringUtil;
import net.cyue.web.easyquery.core.config.ConfigException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class DBConfigurationUtil {

    private enum ConfigName {
        DRIVER,
        URL,
        USERNAME,
        PASSWORD,
        DATASOURCE,
    }


    private static final Logger LOGGER = LoggerFactory.getLogger(DBConfigurationUtil.class);
    private static final Map<ConfigName, List<String>> CONFIGURATION_NAME_MAP = new HashMap<>();
    static {
        CONFIGURATION_NAME_MAP.put(
            ConfigName.DRIVER,
            Arrays.asList("driver", "driverName", "driverClassName")
        );
        CONFIGURATION_NAME_MAP.put(
            ConfigName.URL,
            Arrays.asList("url", "jdbcUrl")
        );
        CONFIGURATION_NAME_MAP.put(
            ConfigName.USERNAME,
            Arrays.asList("username", "user")
        );
        CONFIGURATION_NAME_MAP.put(
            ConfigName.PASSWORD,
            Arrays.asList("password", "pass")
        );
        CONFIGURATION_NAME_MAP.put(
            ConfigName.DATASOURCE,
            Arrays.asList(
                "datasource",
                "datasourceClass",
                "datasourceClassName",
                "dataSource",
                "dataSourceClass",
                "dataSourceClassName"
            )
        );
    }


    private static String getConfigurationValue(Properties properties, ConfigName key) {
        List<String> nameList = CONFIGURATION_NAME_MAP.get(key);
        for (String name : nameList) {
            String value = properties.getProperty(name);
            if (value != null) {
                return value;
            }
        }
        return null;
    }

    public static String getDBType() {
        return "jdbc";
    }

    public static String getMySQLDriverName() {
        return "com.mysql.cj.jdbc.Driver";
    }

    /**
     * 加载resources中的配置文件
     *
     * @param fileName 配置文件名
     * @return properties
     */
    public static DBConfiguration loadProperties(String fileName)
        throws IOException, ConfigException
    {
        Properties properties = new Properties();
        try (InputStream inputStream = ResourceUtil.getResourceAsStream(fileName)) {
            if (inputStream == null) {
                throw new ConfigException(fileName + " 未找到");
            }
            properties.load(inputStream);
        }

        String driver = getConfigurationValue(properties, ConfigName.DRIVER);
        String url = getConfigurationValue(properties, ConfigName.URL);
        String username = getConfigurationValue(properties, ConfigName.USERNAME);
        String password = getConfigurationValue(properties, ConfigName.PASSWORD);
        String datasource = getConfigurationValue(properties, ConfigName.DATASOURCE);

        if (url == null || username == null) {
            LOGGER.warn("{} 未正确配置。", fileName);
        }
        if (StringUtil.isBlank(url)) {
            throw new ConfigException("url为空");
        }
        if (StringUtil.isBlank(username)) {
            throw new ConfigException("username为空");
        }
        if (StringUtil.isBlank(password)) {
            password = "";
        }

        return DBConfiguration
            .builder()
            .driverName(driver)
            .url(url)
            .username(username)
            .password(password)
            .datasource(datasource)
            .build();
    }
}
